package com.github.ylt.utils;

import java.text.DecimalFormat;
import java.time.Duration;
import java.time.LocalTime;

public class TimeUtils {
    private TimeUtils() {}
    private static class SingletonHolder {
        private static final TimeUtils INSTANCE = new TimeUtils();
    }
    public static TimeUtils getInstance() {
        return SingletonHolder.INSTANCE;
    }
    public String timeToString(long time) {
        Duration duration = Duration.ofSeconds(time);
        LocalTime localTime = LocalTime.MIN;
        localTime = localTime.plusHours(duration.toHours());
        localTime = localTime.plusMinutes(duration.toMinutesPart());
        localTime = localTime.plusSeconds(duration.toSecondsPart());

        DecimalFormat decimalFormat = new DecimalFormat("00");
        String hourStr = decimalFormat.format(localTime.getHour());
        String minuteStr = decimalFormat.format(localTime.getMinute());
        String secondStr = decimalFormat.format(localTime.getSecond());

        return hourStr + ":" + minuteStr + ":" + secondStr;
    }
    public long timeToLong(String time) {
        String[] times = time.split(":");
        int hour = Integer.parseInt(times[0]);
        int minute = Integer.parseInt(times[1]);
        int second = Integer.parseInt(times[2]);
        return hour * 3600L + minute * 60L + second;
    }
}
